﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wms.Common;
using wms.Model;

namespace wms.Pages
{
    /// <summary>
    /// Category.xaml 的交互逻辑
    /// </summary>
    public partial class Type : Window
    {
        public TypeModel Value { get; set; }
        private DataTable dt;
        private TypeModel root;

        public Type()
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
        }

        private void loadTree(TypeModel node) {
           
            DataRow[] childrenRows = dt.Select("pid = " + node.Id);
            foreach (DataRow row in childrenRows)
            {
                TypeModel child = new TypeModel((int)row["id"], row["name"] as string);
                child.Parent = node;
                node.Childern.Add(child);
                loadTree(child);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string sql = "select id,pid,name,fullname from t_type";
            dt = DBHelper.GetDataTable(sql, new Dictionary<string, object> ());
            root = new TypeModel(-1, "所有分类");
            loadTree(root);

            ObservableCollection<TypeModel> list = new ObservableCollection<TypeModel>();
            list.Add(root);

            this.treeView.ItemsSource = list;

            treeView_SelectedItemChanged(null,null);
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TypeModel item = this.treeView.SelectedItem as TypeModel;


            if (item == null)
            {
                this.btn_selected.IsEnabled = false;
                this.btn_add_brother.IsEnabled = false;
                this.btn_add_child.IsEnabled = false;
                this.btn_modify.IsEnabled = false;
                this.btn_delete.IsEnabled = false;
                this.Value = null;
            }
            else {

                if (item.Id == -1)
                {
                    this.btn_selected.IsEnabled = false;
                    this.btn_add_brother.IsEnabled = false;
                    this.btn_add_child.IsEnabled = true;
                    this.btn_modify.IsEnabled = false;
                    this.btn_delete.IsEnabled = false;
                    this.Value = null;
                }
                else
                {
                    this.btn_selected.IsEnabled = true;
                    this.btn_add_brother.IsEnabled = true;
                    this.btn_add_child.IsEnabled = true;
                    this.btn_modify.IsEnabled = true;
                    this.btn_delete.IsEnabled = true;
                    this.Value = item;
                }
            }
        }

        private void btn_selected_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void btn_add_brother_Click(object sender, RoutedEventArgs e)
        {
            TypeNameDialog win = new TypeNameDialog();
            bool? dialogResult = win.ShowDialog();
            if (dialogResult != null && dialogResult == true) {
                string text = win.Value;
                if (string.IsNullOrWhiteSpace(text)) {
                    MessageBox.Show("参数无效");
                }
                else
                {
                    TypeModel item = this.Value.Parent;
                    int? id = DBHelper.Insert("insert into t_type(pid,name) values(@pid,@name)",
                    new Dictionary<string, object> { { "@pid", item.Id }, { "@name", text } });
                    if (id != null)
                    {
                        TypeModel newItem = new TypeModel((int)id, text);
                        newItem.Parent = item;
                        item.Childern.Add(newItem);
                    }
                }
            }
        }

        private void btn_add_child_Click(object sender, RoutedEventArgs e)
        {
            TypeNameDialog win = new TypeNameDialog();
            bool? dialogResult = win.ShowDialog();
            if (dialogResult != null && dialogResult == true)
            {
                string text = win.Value;
                if (string.IsNullOrWhiteSpace(text))
                {
                    MessageBox.Show("参数无效");
                }
                else
                {
                    TypeModel item = this.Value;
                    if (item == null) {
                        item = this.root;
                    }

                    int? id = DBHelper.Insert("insert into t_type(pid,name) values(@pid,@name)",
                        new Dictionary<string, object> { { "@pid", item.Id}, { "@name", text }});
                    if (id!=null) {
                        TypeModel newItem = new TypeModel((int)id, text);
                        newItem.Parent = item;
                        item.Childern.Add(newItem);
                    }
                }
                  
            }
        }

        private void btn_modify_Click(object sender, RoutedEventArgs e)
        {
            TypeNameDialog win = new TypeNameDialog();
            bool? dialogResult = win.ShowDialog();
            if (dialogResult != null && dialogResult == true)
            {
                string text = win.Value;
                if (string.IsNullOrWhiteSpace(text))
                {
                    MessageBox.Show("参数无效");
                }
                else
                {
                    TypeModel item = this.Value;
                    if (item != null)
                    {
                        int count = DBHelper.ExcuteSQL("update t_type set name = @name where id = @id", 
                            new Dictionary<string, object> { { "@id", item.Id }, { "@name", text } });
                        if (count > 0) {
                            item.Name = text;
                        }
                    }
                }
            }
        }

        private void btn_delete_Click(object sender, RoutedEventArgs e)
        {
            TypeModel item = this.Value;
            if (item != null)
            {
                int count = DBHelper.ExcuteSQL("delete t_type where id = @id",
                           new Dictionary<string, object> { { "@id", item.Id }});
                if (count > 0) {
                    item.Parent.Childern.Remove(item);
                    item.Parent = null;
                }
            }
        }
    }
}
